/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.Player;

/**
 *
 * @author Eduardo
 */
public class MP3Player
{

    public void play(final String filename) throws JavaLayerException {
        Thread t = new Thread()
        {

            @Override
            public void run()
            {
                try
                {
                    //FileInputStream fin = new FileInputStream();
                    InputStream input = ClassLoader.getSystemResourceAsStream(filename);
                    BufferedInputStream bin = new BufferedInputStream(input);
                    AudioDevice dev = FactoryRegistry.systemRegistry().createAudioDevice();
                    player = new Player(bin, dev);
                    player.play();
                }
                catch (JavaLayerException ex)
                {
                    Logger.getLogger(MP3Player.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };

        t.start();
    }

    public void stop() {
        if (player != null) {
            player.close();
        }
    }

    private Player player;
}
